import 'package:flutter/material.dart';
import 'package:flutter_module/beans/data_layer_bean.dart';
import 'package:flutter_module/beans/project_bean.dart';
import 'package:flutter_module/system/functions.dart';
import 'package:flutter_module/viewModels/layer_edit_viewmodel.dart';
import 'package:flutter_module/views/fragments/layer_edit_fragment.dart';
import 'package:flutter_module/views/resources_config.dart';
import 'package:provider/provider.dart';

class LayersManagerFragment extends StatelessWidget {
  const LayersManagerFragment({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<LayerEditViewModel>(
            create: (_) => LayerEditViewModel()),
      ],

      ///由于路由跳转，provider 要包裹 MaterialApp ，跳转的页面才能共享viewmodel
      child: MaterialApp(home: _ProviderView(
        callBack: () {
          Navigator.pop(context);
        },
      )),
    );
  }
}

class _ProviderView extends StatelessWidget {
  final PopCallBack callBack;

  const _ProviderView({required this.callBack, Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ///viewmodel
    late final LayerEditViewModel _read =
    Provider.of<LayerEditViewModel>(context, listen: false);
    _read.getDataLayerList();

    return Scaffold(
      appBar: AppBar(
        leading: BackButton(
          onPressed: () {
            callBack.call();
          },
        ),
      ),
      body: Selector<LayerEditViewModel, List<Project>>(
        selector: (context, provider) => provider.projectList,
        child: Container(),
        builder: (context, list, child) {
          List<Widget> widgets = [];
          Map<String, List<DataLayer>> map = {};
          for (var item in _read.dataLayerList) {
            if (map.containsKey(item.projectId)) {
              map[item.projectId]?.add(item);
            } else {
              List<DataLayer> list = [];
              list.add(item);
              map.putIfAbsent(item.projectId, () => list);
            }
          }

          for (var item in _read.projectList) {
            List<DataLayer>? layers = map[item.uuid];
            List<Widget> widgetList = [];
            if (layers != null) {
              for (var dataLayer in layers) {
                widgetList.add(
                  ListTile(
                    title: Text(dataLayer.layerName),
                    leading: ResourcesConfig.getGeometryTypeIcon(
                        dataLayer.geometryType),
                    trailing: const Icon(Icons.navigate_next_outlined),
                    onTap: () {
                      Navigator.of(context)
                          .push(MaterialPageRoute(builder: (builder) {
                        _read.initProject(item);
                        _read.initLayer(dataLayer: dataLayer);
                        return const LayerSettingFragment(bInitProvider: false,);
                      }));
                    },
                  ),
                );
              }
            }
            widgets.add(
              Card(
                color: Colors.blue,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(8.0)),
                elevation: 8,
                child: ExpansionTile(
                  leading: const Icon(Icons.home),
                  subtitle: Text(item.describe),
                  backgroundColor: Colors.white,
                  title: Text(item.name),
                  children: widgetList,
                ),
              ),
            );
          }

          return ListView.builder(
              itemCount: widgets.length,
              itemBuilder: (context, index) {
                return widgets[index];
              });
        },
      ),
    );
  }
}
